package cn.cdiothree.controller;

import cn.cdiothree.dto.OrderDto;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.service.ProductOrderService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 卖家端订单
 * @author cumthing
 */

@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellOrderController {
    @Autowired
    private ProductOrderService productOrderService;

    /**
     * 订单列表
     * @param page 第几页，从1页开始
     * @param size 一页有多少条
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        PageInfo<OrderDto> orderDtoPage=productOrderService.findList(page-1,size);
        map.put("orderDTOPage",orderDtoPage);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("order/list",map);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){
        try {
            OrderDto orderDTO=productOrderService.findOne(orderId);
            productOrderService.cancel(orderDTO);
        }catch (SellException e){
            log.error("【卖家端取消订单】发生异常",e);
            map.put("msg", e.getMessage());
            map.put("url","/cumthing/seller/order/list");
            return new ModelAndView("common/error",map);
        }

        map.put("msg",ResultEnum.ORDER_CANCEL_SUCESS.getMessage() );
        map.put("url","/cumthing/seller/order/list");
        return new ModelAndView("common/success");
    }

    /**
     * 订单详情
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){
        OrderDto orderDTO=new OrderDto();
        try {
            orderDTO=productOrderService.findOne(orderId);
        }catch (SellException e){
            log.error("【卖家端查询订单详情】发生异常",e);
            map.put("msg", e.getMessage());
            map.put("url","/cumthing/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("orderDTO",orderDTO);
        return new ModelAndView("order/detail",map);
    }

    /**
     * 完结订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finished(@RequestParam("orderId") String orderId,
                                 Map<String,Object> map) {
        try {
            OrderDto orderDTO = productOrderService.findOne(orderId);
            productOrderService.finish(orderDTO);
        } catch (SellException e) {
            log.error("【卖家端完结订单】发生异常", e);
            map.put("msg", e.getMessage());
            map.put("url", "/cumthing/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url", "/cumthing/seller/order/list");
        return new ModelAndView("common/success");
    }
}
